﻿using MQTTnet.Client;
using MQTTnet.Protocol;
using System;
using System.Collections.Generic;
using System.Security.Authentication;

namespace TK.Mqtt.Client
{
    /// <summary>
    /// mqtt config
    /// </summary>
    public class MqttConfig
    {
        /// <summary>
        /// mqtt config list
        /// </summary>
        public List<MqttClientConfig> ConfigList { get; set; } = new List<MqttClientConfig>();
    }

    /// <summary>
    /// mqtt client config
    /// </summary>
    public class ClientConfig
    {
        /// <summary>
        /// mqtt server host
        /// </summary>
        public string Host { get; set; }

        /// <summary>
        /// mqtt server host's port
        /// </summary>
        public int Port { get; set; }

        /// <summary>
        /// mqtt server clientid
        /// </summary>
        public string ClientId { get; set; }

        /// <summary>
        /// mqtt server user
        /// </summary>
        public string User { get; set; }

        /// <summary>
        /// mqtt QOS level
        /// </summary>
        public MqttQualityOfServiceLevel QOSLevel { get; set; } = MqttQualityOfServiceLevel.AtLeastOnce;

        /// <summary>
        /// key
        /// </summary>
        public string Key => $"{Host}|{Port}|{ClientId}";
    }

    /// <summary>
    /// mqtt client config
    /// </summary>
    public class MqttClientConfig : ClientConfig
    {
        /// <summary>
        /// mqtt server password
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// mqtt client topic list
        /// </summary>
        public List<string> TopicList { get; set; } = new List<string>();

        /// <summary>
        /// 是否启用安全链接
        /// </summary>
        public bool IsTls { get; set; }

        /// <summary>
        /// 安全协议，默认Tls12
        /// </summary>
        public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls12;

        /// <summary>
        /// 安全连接证书路径,相对于程序根目录
        /// </summary>
        public string CertificatePath { get; set; }

        /// <summary>
        /// mqtt server user;委托存在，优先使用委托值
        /// </summary>
        public Func<string> UserFunc { get; set; }

        /// <summary>
        /// mqtt server password;委托存在，优先使用委托值
        /// </summary>
        public Func<string> PasswordFunc { get; set; }

        /// <summary>
        /// 重新自定义MQTT CLIENT连接配置
        /// </summary>
        public Action<MqttClientOptionsBuilder> ConnectionAction { get; set; }
    }
}
